
import jwt

class MyJwt:
    def __init__(self):
        self.secret_ket='2345353244fdfdsfdsfd'
    #生成tokren
    def jwt_encode(self,data):
        return jwt.encode(data,self.secret_ket,algorithm='HS256')
    #解析token
    def jwt_decode(self,token):
        try:
            payload = jwt.decode(token,self.secret_ket,algorithms='HS256')
        except:
            payload = None
        return payload
    #对比token
    def check_token(self,token):
        #解析payload
        payload = self.jwt_decode(token)
        if payload:
            #用payload重新生成token
            newtoken = self.jwt_encode(payload)
            if token == newtoken:
                return True
        return False

mjwt = MyJwt()
# data ={'data':{'userid':123,'exp':int(time.time())}}
# token = mjwt.jwt_encode(data)
# print(token)
# payload = mjwt.jwt_decode(token)
# print(payload)
# flag =mjwt.check_token(token)
# print(flag)